package com.example.mayn.secondmvp.view;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mayn.secondmvp.R;
import com.example.mayn.secondmvp.presenter.MainPresenter;

public class MainActivity extends AppCompatActivity implements IView {

    private TextView hello;
    private MainPresenter presenter;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    hello.setText((String) msg.obj);
                    break;
                case 2:
                    Toast.makeText(MainActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hello = findViewById(R.id.hello);
        presenter = new MainPresenter(this);
        presenter.LoadData();
        presenter.setData2Presenter("aaa");
    }

    @Override
    public void setData2View(String data) {
        Message message = Message.obtain();
        message.what = 1;
        message.obj = data;
        handler.sendMessage(message);
    }

    @Override
    public void setErrorData2View(String data) {
        Message message = Message.obtain();
        message.what = 2;
        message.obj = data;
        handler.sendMessage(message);
    }

}
